/**

 * Create a new StringBuffer instance.
 * A string buffer implements a mutable string, it can be modified.
 * @author	Wan Changhua
 * @author	DSONet
 * @version	$Id: StringBuffer.js 35 2008-03-02 17:01:34Z dsonet $

 * @constructor
 * @final
 * @return {object} A new StringBuffer instance
 */
function StringBuffer (s)
{
	/**
	 * @private
	 */
	var strs = []
		, str = null;

	/**
	 * Appends the string to this string buffer. 
	 * @id StringBuffer.prototype.append
	 * @typeof function
	 * @public
	 * @param {String} s a string
	 * @return {StringBuffer} a reference to this <code>StringBuffer</code>.
	 */
	this.append = function(s)
	{
		strs.push(s);
		str = null;
		return this;
	}

	/**
	 * Inserts the string representation of a subarray of the <code>str</code>
	 * @id StringBuffer.prototype.insert
	 * @typeof function
	 * @public
	 * @param {int} index position at which to insert subarray.
	 * @param {String} s a string.
	 * @return  This string buffer.
	 * @type StringBuffer
	 */
	this.insert = function(index, s)
	{
		var v = this.toString();
		str = v.substring(0, index) 
			+ s + v.substring(index);
		strs = [str];
		return this;
	}
	/**
	 * replace the string representation of a subarray of the <code>str</code>
	 * @id StringBuffer.prototype.replace
	 * @typeof function
	 * @public
	 * @param {String} sToReplace position at which to insert subarray.
	 * @param {String} sReplaceWith a string.
	 * @return  This string buffer.
	 * @type StringBuffer
	 */
	this.replace = function(sToReplace, sReplaceWith)
	{
		var v = this.toString();
		str = v.replace(sToReplace, sReplaceWith);
		strs = [str];
		return this;
	};

	/**

	 * Converts to a string representing the data in this string buffer.

	 * A new String object is allocated and initialized to
	 * contain the character sequence currently represented by this
	 * string buffer. This String is then returned. Subsequent
	 * changes to the string buffer do not affect the contents of the String.
	 * @id StringBuffer.prototype.toString
	 * @typeof function
	 * @public
	 * @return  a string representation of the string buffer.
	 * @type String
	 */
	this.toString = function()
	{
		if (str == null)
		{
			str = strs.join("");
			strs = [str];
		}
		return str;
	}

	/**
	 * @id StringBuffer.prototype.setLength
	 * @typeof function
	 * @public
	 * Sets the length of this String buffer.
	 * @param {number} l the new length of the buffer.
	 * @see #length()
	 */
	this.setLength = function(l)
	{
		l = (l < 0) ? 0 : l;
		var len = this.getLength();
		l = (l > len) ? len : l;
		str = str.substring(0, l);
		strs = [str];
	}
	
	/**
	 * First thing---append the argument
	 * @ignore
	 */
	if (s != null)
	{
		this.append(s);
	}
}

var $p = StringBuffer.prototype;
/**
 * Returns the length (character count) of this string buffer.
 * @public
 * @id StringBuffer.prototype.getLength
 * @return {number} the length of the sequence of characters currently 
 *		  represented by this string buffer.
 */
$p.getLength = function()
{
	return this.toString().length;
}

/**
 * Returns the index within this string of the first occurrence of the
 * specified substring.
 * @param {String} s any string.
 * @return  if the string argument occurs as a substring within this
 * 			object, then the index of the first character of the first
 *		  such substring is returned;
 * @type int
 */
$p.indexOf = function (s)
{
	return this.toString().indexOf(s);
}

/**
 * Returns the index within this string of the rightmost occurrence
 * of the specified substring.
 * @param {String} s any string.
 * @return  if the string argument occurs one or more times as a substring
 *		  within this object, then the index of the first character of
 *		  the last such substring is returned. If it does not occur as
 *		  a substring, <code>-1</code> is returned.
 * @type int
 */
$p.lastIndexOf = function (s)
{
	return this.toString().lastIndexOf(s);
}


$p.match = function (re)
{
	return this.toString().match(re);
}

/**
 * Returns a new String that contains a subsequence of
 * characters currently contained in this <code>StringBuffer</code>.The 
 * substring begins at the specified index and extends to the end of the
 * <code>StringBuffer</code>.
 * @param  start The beginning index, inclusive.
 * @return The new string.
 * @type int
 */
$p.substring = function (st, ed)
{
	return this.toString().substring(st, ed);
}

$p = null;